package com.wanglibao.woodtrade.woodtrade.ui.activity.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wanglibao.woodtrade.woodtrade.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ItemImgvRvCommodityAdapter extends BaseAdapter {
    // 存放图片的url
    public List<String> objects = new ArrayList<>();
    // 存放图片的名称 上传的时候要使用
    public  List<String> mPicName=new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;
    Drawable mDrawable;
    int Postion_pic;

    public ItemImgvRvCommodityAdapter(Context context, List<String> list,List<String> picName) {
        this.context = context;
        objects = list;
        objects.add("");
        this.mPicName=picName;
        mDrawable=context.getResources().getDrawable(R.mipmap.ic_edit_bigadd);
        this.layoutInflater = LayoutInflater.from(context);
    }

    public ItemImgvRvCommodityAdapter(Context context, List<String> list,List<String> picName,boolean isShowAdd) {
        this.context = context;
        objects = list;
        if (isShowAdd){
            objects.add("");
        }
        this.mPicName=picName;
        mDrawable=context.getResources().getDrawable(R.mipmap.ic_edit_bigadd);
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void removeData(int position) {
        objects.remove(position);
        mPicName.remove(position);
        notifyDataSetChanged();
    }

    public void addData(int position, String string) {
        objects.add(position, string);
        notifyDataSetChanged();
    }
    public void addDataTomPicName(int position,String string) {
        mPicName.add(position,string);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(objects.size()>6){
            return 6;
        }else {
            return objects.size();
        }
    }

    @Override
    public String getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_imgv_rv_commodity, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((String) getItem(position), (ViewHolder) convertView.getTag(),position);
        return convertView;
    }

    private void initializeViews(final String object, ViewHolder holder, final int position) {
        //TODO implement
        if(position==objects.size()-1){
            holder.imgvDeleteSvgw.setVisibility(View.INVISIBLE);
            holder.sdvItemRv.setImageDrawable(mDrawable);

        }else {
            holder.imgvDeleteSvgw.setVisibility(View.VISIBLE);
            holder.sdvItemRv.setImageURI(Uri.parse(object));
        }
        holder.sdvItemRv.setClickable(false);
        holder.imgvDeleteSvgw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeData(position);
            }
        });
    }

    public static class ViewHolder {
        @Bind(R.id.sdv_item_rv)
         SimpleDraweeView sdvItemRv;
        @Bind(R.id.imgv_delete_svgw)
         ImageView imgvDeleteSvgw;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
